﻿/// <summary>
/// 
/// </summary>
//using Oracle.DataAccess.Client;
using System.Data;
using System;
using System.Collections.Generic;
using Framework.Data.Exceptions;

namespace Framework.Data.Core
{
    /// <summary>
    /// Classe qui enveloppe un entité dans les AttachedContainer
    /// </summary>
    /// <remarks></remarks>
    public class AttachedEntity : IBaseEntity
    {

        #region "Membres"

        protected IBaseEntity _entity = null;
        protected bool _isNew = false;

        protected bool _MarkAsDeleted = false;
        #endregion

        #region "Constructeurs"

        /// <summary>
        /// Constructeurs
        /// </summary>
        /// <remarks></remarks>
        public AttachedEntity(IBaseEntity entity)
        {
            this._entity = entity;
            this._isNew = true;
        }
        #endregion

        #region "Property"
        public IBaseEntity Entity
        {
            get { return this._entity; }
            set { this._entity = value; }
        }
        #endregion

        #region "IBaseEntity"
        /// <summary>
        /// Function qui permet de dupliquer un entité
        /// </summary>
        /// <returns>IBaseEntity</returns>
        /// <remarks></remarks>
        public IBaseEntity Duplicate(bool duplicateChildren, IDbConnection connection = null)
        {
            throw new NotImplementedException("Vous ne pouvez pas créer un entité à partir d'un AttachEntity");
        }

        public void AddField(string key, IField field)
        {
            this._entity.AddField(key, field);
        }

        public System.Collections.Generic.Dictionary<string, AttachedContainer> AttachedContainers
        {
            get { return this._entity.AttachedContainers; }
        }

        public System.Collections.Generic.Dictionary<string, ChildrenContainer> ChildrenContainers
        {
            get { return this._entity.ChildrenContainers; }
        }

        public System.Collections.Generic.Dictionary<string, IField> Fields
        {
            get { return this._entity.Fields; }
        }

        public object HashCode
        {
            get { return this._entity.HashCode; }
        }

        public IField GetField(string fieldName)
        {
            return this._entity.GetField(fieldName);
        }

        public IField GetPrimaryKeyField()
        {
            return this._entity.GetPrimaryKeyField();
        }

        public void InitializeData(System.Data.IDataReader dataReader)
        {
            this._entity.InitializeData(dataReader);
        }

        public void InitializeData(System.Data.DataColumnCollection dataColumns, DataRow dataRow)
        {
            this._entity.InitializeData(dataColumns, dataRow);
        }

        public bool IsNew
        {
            get { return this._isNew; }
            set { this._isNew = value; }
        }

        /// <summary>
        /// Fonction qui permet de valider un entité
        /// </summary>
        /// <returns>List(Of AppException)</returns>
        /// <remarks></remarks>
        public virtual List<AppException> ValidateRules()
        {
            return new List<AppException>();
        }

        /// <summary>
        /// Fonction qui valide si on peut supprimer l'entité
        /// </summary>
        /// <param name="deleteException">Exception sur le delete</param>
        /// <returns>Boolean</returns>
        /// <remarks></remarks>
        public virtual bool CanDelete(ref AppException deleteException)
        {
            return true;
        }

        /// <summary>
        /// Fonction qui retourne le type de l'entité
        /// </summary>
        /// <returns>System.Type</returns>
        /// <remarks></remarks>
        public new System.Type GetType()
        {
            return this._entity.GetType();
        }

        /// <summary>
        /// Fonction qui permet d'afficher l'instance de cette classe sous forme de chaîne de caractère
        /// </summary>
        /// <returns>String</returns>
        /// <remarks></remarks>
        public override string ToString()
        {
            return this._entity.ToString();
        }

        /// <summary>
        /// Fonction qui retourne le tri par défaut de l'entité
        /// </summary>
        /// <returns>String</returns>
        /// <remarks></remarks>
        public string GetDefaultSortOrder()
        {
            return this._entity.GetDefaultSortOrder();
        }
        #endregion

        #region "IEditableEntity"
        public void AcceptChanges(bool updateIsNew = true)
        {
            this._isNew = !updateIsNew;
            this._MarkAsDeleted = false;
        }

        public bool Cancel()
        {
            this._isNew = false;
            this._MarkAsDeleted = false;
            return true;
        }

        public bool Delete(IDbConnection connection = null)
        {
            this._isNew = false;
            this._MarkAsDeleted = false;
            return true;
        }

        public bool IsDirty(bool checkFieldOnly = false)
        {
            bool result = false;
            if (!checkFieldOnly)
            {
                result = (this._isNew | this._MarkAsDeleted);
            }
            return result;
        }

        public bool MarkAsDeleted
        {
            get { return this._MarkAsDeleted; }
            set { this._MarkAsDeleted = value; }
        }

        public void RejectChanges()
        {
            this._isNew = false;
            this._MarkAsDeleted = false;
        }

        public bool Save(IDbConnection connection = null)
        {
            return true;
        }

        public bool Save(IDbTransaction transaction, IDbConnection connection = null)
        {
            return true;
        }
        #endregion

        #region "IReadOnly"
        public bool ReadOnly
        {
            get { return this._entity.ReadOnly; }
            set { this._entity.ReadOnly = value; }
        }
        #endregion


    }
}